// pages/exam-question/index.js
import {
    request_post
} from '../../utils/request';
import Toast from '@vant/weapp/toast/toast';
const app = getApp()

Page({
    data: {
        loaded: false,
        question: null,
        checkedOptions: [],
        currentPage: 0,
        options: null,
        answeredQuestion: [],
        answeredSuccessCount: 0,
        answeredFailedCount: 0,
        examDetailShow: false,
        seqId: null, //本次答题唯一标识
    },

    onLoad(options) {
        this.setData({options: options})
        this.loadNextQuestion(null, true)
    },
    loadNextQuestion(event, firstLoad) {
        this.setData({
            examDetailShow: false
        })
        if (this.data.answeredQuestion.length >= this.data.currentPage + 1) {
            this.setData({
                question: this.data.answeredQuestion[this.data.currentPage],
                currentPage: this.data.currentPage + 1
            })
            return
        }

        request_post('/question/list-for-show/1/1', {
            basicCode: this.data.options.code,
            type: this.data.options.type,
            clearPreviousAnswerProcess: this.data.options.clearPreviousAnswerProcess,
            fromWrongBook: this.data.options.fromWrongBook,
            seqId: this.data.seqId
        }).then(res => {
            this.data.options.clearPreviousAnswerProcess = false
            let question = res.data && res.data.length > 0 ? res.data[0] : null
            if (question) {
                let question_question = question.question.split('____')
                if (question.answers) {
                    question.answers.forEach((answer, index) => {
                        answer.abcd = String.fromCharCode(64 + index + 1)

                        if (this.data.options.type == 'TIANKONG') {
                            question_question.splice(index * 2 + 1, 0, '<span style="border-bottom: 1px solid #00B5FC;color: transparent;">&nbsp;&nbsp;' + answer.answer + '&nbsp;&nbsp;</span>')
                        }
                        if (this.data.options.type == 'MINGCIJIESHI' || this.data.options.type == 'LUNSHU') {
                            answer.show = false
                        }
                    });
                }
                question.nextAnswerIndex = 0
                question.question = question_question.join('')
                this.data.answeredQuestion.push(question)
                this.setData({
                    currentPage: this.data.currentPage + 1,
                    question: question,
                    answeredQuestion: this.data.answeredQuestion,
                    loaded: true
                })
                return
            }

            this.setData({
                question: null,
                loaded: true
            })

        })
    },
    loadPrevQuestion() {
        if (this.data.currentPage == 1) {
            this.setData({
                question: this.data.answeredQuestion[1]
            })
            return
        }

        this.data.currentPage--
        this.setData({
            question: this.data.answeredQuestion[this.data.currentPage - 1],
            currentPage: this.data.currentPage
        })
    },
    onDanXuanAnswer(event) {
        this.data.question.answers.forEach((answer, index) => {
            answer.checked = index == event.currentTarget.dataset.answerindex
        })
        this.data.question.answerRights = event.currentTarget.dataset.rights
        this._commitLocalAnswer(this.data.question.answerRights)

        request_post('/exam/answer', {
            questionId: this.data.question.id,
            answerId: [event.currentTarget.dataset.answerid],
            basicCode: this.data.options.code,
            type: this.data.options.type,
            rights: this.data.question.answerRights,
            seqId: this.data.seqId,
            fromWrongBook: this.data.options.fromWrongBook,
            removeOnAnswerRight: app.globalData.student.removeOnAnswerRight
        }).then(res => {
            this.data.seqId = res
            if (this.data.question.answerRights) this.loadNextQuestion()
        })
    },
    onDuoXuanAnswer(event) {
        this.data.question.answers.forEach((answer, index) => {
            if (index == event.currentTarget.dataset.answerindex) {
                answer.checked = !answer.checked
            }
        })
        this._changeCurrentQuestion()
    },
    commitDuoXuanAnswer() {
        this.data.question.answerRights = !this.data.question.answers.some(answer => (answer.rights && !answer.checked) || (!answer.rights && answer.checked))
        this._commitLocalAnswer(this.data.question.answerRights)

        request_post('/exam/answer', {
            questionId: this.data.question.id,
            answerId: this.data.question.answers.filter(answer => answer.checked).map(answer => answer.id),
            basicCode: this.data.options.code,
            type: this.data.options.type,
            rights: this.data.question.answerRights,
            seqId: this.data.seqId,
            fromWrongBook: this.data.options.fromWrongBook,
            removeOnAnswerRight: app.globalData.student.removeOnAnswerRight
        }).then(res => {
            this.data.seqId = res
            if (this.data.question.answerRights) this.loadNextQuestion()
        })
    },
    _commitLocalAnswer(rights) {
        if (this.data.question.answerCommitted) return

        if (rights) {
            this.setData({
                answeredSuccessCount: this.data.answeredSuccessCount + 1
            })
        } else {
            this.setData({
                answeredFailedCount: this.data.answeredFailedCount + 1
            })
        }

        this.data.question.answerCommitted = true
        this._changeCurrentQuestion()
    },
    _changeCurrentQuestion() {
        this.setData({
            question: this.data.question,
            currentPage: this.data.currentPage
        })
        this.data.answeredQuestion[this.data.currentPage - 1] = this.data.question
        this.setData({
            answeredQuestion: this.data.answeredQuestion
        })
    },
    toggleExamChoose() {
        this.setData({
            examDetailShow: !this.data.examDetailShow
        })
    },
    loadSpecificQuestion(event) {
        this.toggleExamChoose()
        this.data.currentPage = event.currentTarget.dataset.index + 1
        this.data.question = this.data.answeredQuestion[event.currentTarget.dataset.index]
        this._changeCurrentQuestion()
    },
    toggleSaveToWrongBook() {
        this.data.question.stared = !this.data.question.stared
        Toast({
            position: 'bottom',
            message: this.data.question.stared ? '收藏成功, 题目已加入到错题本' : '取消收藏成功, 题目已经从错题本移出'
        });
        this._changeCurrentQuestion()
        request_post('/exam/star-question', {
            questionId: this.data.question.id,
            basicCode: this.data.options.code,
            type: this.data.options.type,
            star: this.data.question.stared
        })
    },
    showNextAnswer() {
        //填空/名词解释/论述题 显示答案,一次一个, 当显示最后一个时, 自动提交答题进度
        if (this.data.question.nextAnswerIndex >= this.data.question.answers.length - 1) {
            this.data.question.answerRights = true
            this._commitLocalAnswer(true)

            request_post('/exam/answer', {
                questionId: this.data.question.id,
                answerId: [],
                basicCode: this.data.options.code,
                type: this.data.options.type,
                rights: this.data.question.answerRights,
                seqId: this.data.seqId,
                fromWrongBook: this.data.options.fromWrongBook,
                removeOnAnswerRight: app.globalData.student.removeOnAnswerRight
            }).then(res => {
                this.data.seqId = res
            })
        }

        if (this.data.options.type == 'TIANKONG') {
            let answer = this.data.question.answers[this.data.question.nextAnswerIndex]
            this.data.question.question = this.data.question.question.replace('<span style="border-bottom: 1px solid #00B5FC;color: transparent;">&nbsp;&nbsp;' + answer.answer + '&nbsp;&nbsp;</span>', '<span style="border-bottom: 1px solid #00B5FC;color: #00B5FC;">&nbsp;&nbsp;' + answer.answer + '&nbsp;&nbsp;</span>')
        } else if (this.data.options.type == 'MINGCIJIESHI' || this.data.options.type == 'LUNSHU') {
            this.data.question.answers[this.data.question.nextAnswerIndex].show = true
        }
        this.data.question.nextAnswerIndex++
        this._changeCurrentQuestion()
    }
})